<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2018/3/20
 * Time: 21:56
 */

namespace app\api\controller\v1;


use app\api\service\UserToken;
use app\api\validate\TokenGet;
use app\lib\exception\ParamterException;
use app\api\service\Token as TokenService;

class Token
{
    /**
     * 用户获取令牌（登录），用户不存在就创建新用户
     * @url /token/user
     * @param string $code
     * @return array
     */
    public function getToken($code = ''){
        (new TokenGet())->goCheck();
        $wx = new UserToken($code);
        $token = $wx->get();
        return [
            'token' => $token
        ];
    }

    /**
     * 检测token是否存在
     * @param string $token
     * @return array
     * @throws ParamterException
     */
    public function verifyToken($token = ''){
        if(!$token){
            throw new ParamterException([
                'msg' => 'token不允许为空'
            ]);
        }

        $valid = TokenService::verifyToken($token);
        return [
            'isValid' => $valid
        ];
    }
}